unit UPlanilha;

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, ULinhas, Grids;

type
  St10 = String[10];
  St100 = String[100];
  TPlanilha = class (TObject)
  private
    FNCols: Integer;
    FNLins: Integer;
    property NCols: Integer read FNCols write FNCols;
    property NLins: Integer read FNLins write FNLins;
  public
    Linhas: Array[1..100] of TLinha;
    TituloColunas: Array[1..100] of ST100;
    TituloLinhas: Array[1..100] of ST100;
    constructor Create;
    procedure GetStream(var ValStream: TMemoryStream);
    procedure RetornaGridDados(var Grid : TStringGrid);
    procedure RetornaGridResultados(var Grid : TStringGrid);
    procedure SetNLinsCols(xNLins,xNCols:integer);
    procedure SetStream(ValStream: TMemoryStream);
  end;


implementation

uses UCelula;

{
********************************** TPlanilha ***********************************
}

Constructor TPlanilha.Create;
var
  i: Word;
begin
  for i := 1 to 100 do  // ajustar para NLins passado como parametro do create
    Linhas[i] := TLinha.Create;
end;

// retorna, em ValStream, uma stream com as propriedades da planilha
procedure TPlanilha.GetStream(var ValStream: TMemoryStream);
var
  i, j: Word;
  Xst : string[10];
  Zst : string[100];
begin
  ValStream.Position := 0;
  ValStream.Write(Ncols,SizeOf(Ncols));
  ValStream.Write(NLins,SizeOf(NLins));
  for i := 1 to Ncols do
   ValStream.Write(TituloColunas[i],SizeOf(TituloColunas[i]));
  for i := 1 to NLins do
   ValStream.Write(TituloLinhas[i],SizeOf(TituloLinhas[i]));
  for i := 1 to NLins do
   for j := 1 to NCols do
     begin
       Xst := Linhas[i].Colunas[j].GetTipo;
       ValStream.Write(Xst,SizeOf(Xst));
       Xst := Linhas[i].Colunas[j].GetValor;
       ValStream.Write(Xst,SizeOf(Xst));
       Zst := Linhas[i].Colunas[j].GetFormula;
       ValStream.Write(Zst,SizeOf(Zst));
     end;
end;

// preenche as propriedades da planilha com os dados recebidos de ValStream
procedure TPlanilha.SetStream(ValStream: TMemoryStream);
var
  Zst: St100;
  Xst : St10;
  N, i, j: Integer;
begin
  ValStream.Position := 0;
  ValStream.Read(N,SizeOf(N));
  NCols := N;
  ValStream.Read(N,SizeOf(N));
  NLins := N;
  for i := 1 to Ncols do
    begin
      ValStream.Read(Zst,SizeOf(Zst));
      TituloColunas[i] := Zst;
    end;
  for i := 1 to NLins do
    begin
      ValStream.Read(Zst,SizeOf(Zst));
      TituloLinhas[i] := Zst;
    end;
  for i := 1 to NLins do
   for j := 1 to NCols do
     begin
       ValStream.Read(Xst,SizeOf(Xst));
       Linhas[i].Colunas[j].SetTipo(Xst);
       ValStream.Read(Xst,SizeOf(Xst));
       Linhas[i].Colunas[j].SetValor(Xst);
       ValStream.Read(Zst,SizeOf(Zst));
       Linhas[i].Colunas[j].SetFormula(Zst);
     end;
end;

procedure TPlanilha.RetornaGridDados(var Grid : TStringGrid);
var
  i, j: Integer;
  st: string;
begin
  Grid.Cells[0,0] := 'Lin\Col';
  for i := 1 to Ncols do
   Grid.Cells[i,0] := TituloColunas[i];
  for i := 1 to NLins do
   Grid.Cells[0,i] := TituloLinhas[i];
  for i := 1 to NLins do
   for j := 1 to NCols do
     begin
       if Linhas[i].Colunas[j].GetTipo = 'formula' then
         Grid.Cells[j,i] :=  Linhas[i].Colunas[j].GetFormula
       else
         Grid.Cells[j,i] :=  Linhas[i].Colunas[j].GetValor;
     end;
end;

procedure TPlanilha.SetNLinsCols(xNLins,xNCols:integer);
begin
  NLins := xNLins;
  NCols := xNCols;
end;



procedure TPlanilha.RetornaGridResultados(var Grid: TStringGrid);
  i, j: Integer;
  st: string;
begin
  Grid.Cells[0,0] := 'Lin\Col';
  for i := 1 to Ncols do
   Grid.Cells[i,0] := TituloColunas[i];
  for i := 1 to NLins do
   Grid.Cells[0,i] := TituloLinhas[i];
  for i := 1 to NLins do
   for j := 1 to NCols do
     Grid.Cells[j,i] :=  Linhas[i].Colunas[j].GetValor;
end;

end.
